package com.roncoo.education.course.service.api.pc.biz;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.roncoo.education.course.common.req.*;

import com.roncoo.education.course.service.dao.impl.mapper.entity.CourseTopic;
import com.roncoo.education.course.service.dao.service.impl.mapper.CourseTopicMapper;
import com.roncoo.education.util.base.Result;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.validation.constraints.Size;
import java.time.LocalDateTime;
import java.util.List;


/**
 * 课程答题 或者问卷
 */
@Component
public class PcApiCourseTopIcBiz {

    @Resource
    private CourseTopicMapper mapper;

    public Result<Page> findPageList(CourseTopicREQ courseTopicREQ){

        Page<CourseTopic> page=new Page<>(courseTopicREQ.getPageCurrent(),courseTopicREQ.getPageSize());
        List<CourseTopic> list=mapper.getPageList(courseTopicREQ,page);
        page.setRecords(list);
        return Result.success(page);


    }


    public Result saveOrmodefy(CourseTopicREQ courseTopicREQ){
        CourseTopic courseTopic= new CourseTopic();
        BeanUtils.copyProperties(courseTopicREQ,courseTopic);
        courseTopic.setGmtModified(LocalDateTime.now());
        courseTopic.insertOrUpdate();
        return Result.success("操作成功");
    }

    public Result delete(CourseTopicREQ courseTopicREQ){
        CourseTopic courseTopic=new CourseTopic();
        courseTopic.setGmtModified(LocalDateTime.now());
        courseTopic.setId(courseTopicREQ.getId());
        courseTopic.setStatusId(0);
        if(mapper.updateById(courseTopic)>0){
            return Result.success("删除成功");
        }
        return Result.error("删除失败");

    }

    /**
     * 修改问卷名称
     * @param courseTopicREQ
     * @return
     */
    public Result update(CourseTopicREQ courseTopicREQ){
        CourseTopic courseTopic=new CourseTopic();
        courseTopic.setGmtModified(LocalDateTime.now());
        courseTopic.setId(courseTopicREQ.getId());
        if(mapper.updateById(courseTopic)>0){
            return Result.success("修改成功");
        }
        return Result.error("修改失败");

    }

    /**
     * 编辑问卷
     * @param courseTopicREQ
     * @return
     */
    public Result updateAll(CourseTopicREQ courseTopicREQ){
        CourseTopic courseTopic=new CourseTopic();
        courseTopic.setGmtModified(LocalDateTime.now());
        courseTopic.setId(courseTopicREQ.getId());
        courseTopic.setTitle(courseTopicREQ.getTitle());
        courseTopic.setDescript(courseTopicREQ.getDescript());
        courseTopic.setType(courseTopicREQ.getType());
        if(mapper.updateById(courseTopic)>0){
            return Result.success("修改成功");
        }
        return Result.error("修改失败");

    }

    /**
     * 跳转页面  编辑问卷
     * @param courseTopicREQ
     * @return
     */
    public Result<CourseTopic> updateList(CourseTopicREQ courseTopicREQ){
        CourseTopic courseTopic=new CourseTopic();
        courseTopic=mapper.selectById(courseTopicREQ.getId());
        if(courseTopic != null){
            return Result.success(courseTopic,"跳转成功");
        }
        return Result.error("修改失败");
    }

}
